/**
 *	@project:	kybernetes
 *	@author:	D.Toepel
 *	@note:		---
 *	
 *	Copyright (C) 2009  D.Toepel <de.dtools> {at} <gmail> {dot} <com>
 *	
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 */
package dt.htwk.rmi;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Inet4Address;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;

/**
 * <h3>BroadCastServer</h3>
 * 
 * @author DToepel
 * 
 */
public class BroadCastServer extends Thread {

	/* ****************************************************
	 * Deklaration der Variablen und Komponenten
	 * **************************************************
	 */

	/**
	 * serialVersionUID -- Standard: 1L
	 */
	private static final long serialVersionUID = 1L;

	private static final int BCPORT = 15177;

	/* ****************************************************
	 * Aufruf des Konstruktors
	 * **************************************************
	 */

	/* ****************************************************
	 * PUBLIC - oeffentliche Methoden
	 * **************************************************
	 */

	public void run() {
		DatagramSocket udpSocket = null;
		String addr = "";
		String ip[] = null;
		int sendID = 1;

		try {
			ip = InetAddress.getLocalHost().getHostAddress().split("\\.");
		} catch (UnknownHostException e1) {
			ip = new String[]{"127","0","0","1"};
		}
		String addr1 = ip[0] + "." + ip[1] + "." + ip[2] + ".255";
		String addr2 = ip[0] + "." + ip[1] + ".0.255";
		String addr3 = "255.255.255.255";
		String msg = "KCServer";
		
		while (true) {
			try {
				udpSocket = new DatagramSocket(BCPORT);
				udpSocket.setBroadcast(true);
				
				while (true) {
					if (sendID == 1){
						System.out.println("------------------------------");
						sleep(2000);
					}

					switch (sendID) {
					case 1:
						addr = addr1;
						break;
					case 2:
						addr = addr2;
						break;
					default:
						addr = addr3;
						sendID = 0;
						break;
					}

					DatagramPacket packet = new DatagramPacket(msg.getBytes(),
							msg.length(), InetAddress.getByName(addr), BCPORT);
					udpSocket.send(packet);

					sendID++;
					System.out.println("Broadcast IP:(" + addr + ")\t# Message: " + msg);
					
//					InetAddress localHost = Inet4Address.getLocalHost();
//					NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
//
//					for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
//						// getNetworkPrefixLength()
//					    System.out.println(address.getBroadcast());
//					}

				}
			} catch (SocketException e) {
				 e.printStackTrace();
			} catch (IOException e) {
				 e.printStackTrace();
			} catch (InterruptedException e) {
				 e.printStackTrace();
			} finally {
				udpSocket.close();
			}
		}
	}

	/* ****************************************************
	 * PRIVATE - nicht-oeffentliche Methoden
	 * **************************************************
	 */
	

}
